package com.cucn.ingmall.ware.controller;

import com.cucn.common.exception.BizCodeEnume;
import com.cucn.common.exception.NoStockException;
import com.cucn.common.to.SkuHasStockVO;
import com.cucn.common.utils.PageUtils;
import com.cucn.common.utils.R;
import com.cucn.ingmall.ware.entity.WareSkuEntity;
import com.cucn.ingmall.ware.service.WareSkuService;
import com.cucn.ingmall.ware.vo.WareSkuLockDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.cucn.common.utils.R.ok;


/**
 * 商品库存
 *
 * @author kouchaojie
 * @email kou1688@foxmail.com
 * @date 2021-11-17 18:35:22
 */
@RestController
@Slf4j
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 锁定sku库存数据
     */
    @PostMapping("/lock/order")
    public R orderLockSkuWare(@RequestBody WareSkuLockDTO wareSkuLockDTO) {
        log.info("订单锁定商品库存..{}", wareSkuLockDTO);
        try {
            //返回每个商品锁定库存成功与否的信息
            boolean lockStockResult = wareSkuService.orderLockSkuWare(wareSkuLockDTO);
            return R.ok().setData(lockStockResult);
        } catch (NoStockException e) {
            log.error("库存锁定失败", e);
            return R.error(BizCodeEnume.NO_STOCK_EXCEPTION.getCode(), BizCodeEnume.NO_STOCK_EXCEPTION.getMsg());
        }
    }

    /**
     * 判断sku是否有库存
     *
     * @param skuIds skuId集合
     */
    @PostMapping("/hasstock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds) {
        //skuId,stock
        List<SkuHasStockVO> vos = wareSkuService.getSkusHasStock(skuIds);

        return R.ok().setData(vos);
    }

    /**
     * sku库存列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPage(params);

        return ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return ok();
    }

}
